package com.hhkj.admin.bank.common.mybatis.handler;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import com.hhkj.admin.bank.model.extend.TestQuestionAmountConf;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;

import java.util.HashMap;
import java.util.Map;

/**
 * TestAmountConfDetailMapHandler
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/6/19 9:47
 */
@Slf4j
@MappedJdbcTypes(JdbcType.VARCHAR)
public class TestAmountConfMapHandler extends AbstractJsonTypeHandler<Map<String, TestQuestionAmountConf>>
{
    @Override
    protected Map<String, TestQuestionAmountConf> parse(String json)
    {
        if (StrUtil.isBlank(json)) {
            return null;
        }
        Map<String, TestQuestionAmountConf> value = new HashMap<>();
        JSONObject jsonObject = JSONObject.parse(json);
        JSONObject correctJson;
        for (Map.Entry<String, Object> stringObjectEntry : jsonObject.entrySet()) {
            correctJson = (JSONObject) (stringObjectEntry.getValue());
            value.put(stringObjectEntry.getKey(),
                    correctJson.to(TestQuestionAmountConf.class)
            );
        }
        return value;
    }

    @Override
    protected String toJson(Map<String, TestQuestionAmountConf> obj)
    {
        return new JSONObject(obj).toJSONString(
                JSONWriter.Feature.WriteNullListAsEmpty,
                JSONWriter.Feature.WriteMapNullValue,
                JSONWriter.Feature.WriteNullStringAsEmpty
        );
    }
}
